﻿//--------------------------------------------------------------------------------------------------------------
// <copyright file="CSPCitizenProfileWUC.cs" company="Microsoft Corporation" author="David Martos" date="01/01/2008">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//--------------------------------------------------------------------------------------------------------------


using System;
using System.Collections;
using System.Collections.Specialized;
using System.Reflection;
using System.Resources;
using System.Runtime.InteropServices;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using Microsoft.Security.Application;

namespace CSP.WebParts.UserControls
{
    /// <summary>
    /// Shows the citizens profile data
    /// </summary>    
    public class CSPCitizenProfileWUC : CSPBaseWUC 
    {
       
        #region Members             
        protected Label lblName;
        protected Label lblAddress;
        protected Label lblPhone;
        protected Label lblEmail;
        protected Button CitizenProfileEditProfile;
        protected Image imgPhoto;
        protected Panel pnlCitizenProfile;
        #endregion

        #region properties
        public string EditProfileUrl { get; set; }
        public string NoImageUrl { get; set; }
        #endregion


        #region PageEvents
        /// <summary>
        /// Fired on page load
        /// </summary>  
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    if (!Page.IsPostBack)
                    {
                        this.LoadLabels();
                        this.LoadData();
                    }
                });
            }
            catch (CSP.Core.CSPException cspex)
            {
                throw cspex;
            }
            catch (Exception ex)
            {
                this.LogMessage(this.LoadResource(WebPartConstants.CSPCitizenProfileLoadErrorMessage), this.GetType().Name, ex);
                throw ex;
            }
        }

        /// <summary>
        /// Fired on page create child controls
        /// </summary>  
        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            try
            {
                this.CitizenProfileEditProfile.Click += new EventHandler(CitizenProfileEditProfile_Click);
            }
            catch (CSP.Core.CSPException cspex)
            {
                throw cspex;
            }
            catch (Exception ex)
            {
                this.LogMessage(this.LoadResource(WebPartConstants.CSPCitizenProfileCreateChildControlsErrorMessage), this.GetType().Name, ex);
                throw ex;
            }            
        }
        #endregion


        #region Methods
        /// <summary>
        /// Sets the value of the labels using the resource file with localized values.
        /// </summary>         
        protected void LoadLabels()
        {
            try
            {
                base.LoadLabels(this.pnlCitizenProfile);
            }
            catch (CSP.Core.CSPException cspex)
            {
                throw cspex;
            }
            catch (Exception ex)
            {
                this.LogMessage(this.LoadResource(WebPartConstants.CSPCitizenProfileLoadLabelsErrorMessage), this.GetType().Name, ex);
                throw ex;
            }

        }

        /// <summary>
        /// Gets the citizen's data
        /// </summary>          
        private void LoadData()
        {
            string username = string.Empty;

            username = Context.User.Identity.Name;

            CSP.MembershipInterface.ObjectModel.Citizen citizen = null;
            CSP.MembershipInterface.MembershipServiceClient client = null;
            try
            {
                client = new CSP.MembershipInterface.MembershipServiceClient();
                citizen = client.GetCitizenByUsername(username);
                    
           
                if (!String.IsNullOrEmpty(citizen.PhotoUrl))
                    this.imgPhoto.ImageUrl = citizen.PhotoUrl;
                else
                    this.imgPhoto.ImageUrl = this.NoImageUrl;

                this.lblName.Text = "";
                if (!String.IsNullOrEmpty(citizen.DisplayName))
                    this.lblName.Text = AntiXss.HtmlEncode(citizen.DisplayName);

                this.lblAddress.Text = "";
                if (!String.IsNullOrEmpty(citizen.Address))
                    this.lblAddress.Text += AntiXss.HtmlEncode(citizen.Address);

                if (!String.IsNullOrEmpty(citizen.City))
                    this.lblAddress.Text += "<br/>" + AntiXss.HtmlEncode(citizen.City);

                if (!String.IsNullOrEmpty(citizen.PostalCode))
                    this.lblAddress.Text +=" " + AntiXss.HtmlEncode(citizen.PostalCode);

                this.lblPhone.Text = "";
                if (!String.IsNullOrEmpty(citizen.Phone))
                    this.lblPhone.Text = AntiXss.HtmlEncode(citizen.Phone);

                this.lblEmail.Text = "";
                if (!String.IsNullOrEmpty(citizen.EmailAddress))
                    this.lblEmail.Text = AntiXss.HtmlEncode(citizen.EmailAddress);

            }   
            catch (CSP.Core.CSPException cspex)
            {
                throw cspex;
            }
            catch (Exception ex)
            {
                this.LogMessage(this.LoadResource(WebPartConstants.CSPCitizenProfileCitizenNotFoundErrorMessage), this.GetType().Name, ex);
                throw ex;
            }
            finally
            {
                if (client != null && client.State != System.ServiceModel.CommunicationState.Closed)
                    client.Close();
            }

        }
    

        /// <summary>
        /// Fired on edit profile button click
        /// </summary>  
        void CitizenProfileEditProfile_Click(object sender, EventArgs e)
        {
            if (!EditProfileUrl.Equals(string.Empty))
                Response.Redirect(EditProfileUrl);
        }
        #endregion                    
    }
}
